package net.guruqu.simpleTest;

import javax.swing.JFrame;

import net.guruqu.rvoView.ModelRepository;

import com.gibbon.jme.context.JmeContext;
import com.gibbon.jme.context.RenderPass;
import com.gibbon.jme.context.lwjgl.LWJGLContext;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.Spatial.LightCombineMode;
import com.jme.scene.Spatial.TextureCombineMode;
import com.jme.scene.state.ZBufferState;

public class ModelPreLoaderTest extends JFrame {
	ModelRepository modelRepository;

	JmeContext context;

	public ModelPreLoaderTest(ModelRepository modelRepository) {
		this.modelRepository = modelRepository;
		try {
			if (modelRepository == null)
				context = JmeContext.create(LWJGLContext.class,
						JmeContext.CONTEXT_CANVAS);
			else
				context = JmeContext.create(LWJGLContext.class,
						JmeContext.CONTEXT_CANVAS, modelRepository
								.getModelContext());

			context.start();
			RenderPass pass = new SimplePass();
			context.getPassManager().add(pass);

			getContentPane().add(context.getCanvas());
			setSize(640, 480);
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			setVisible(true);
			context.waitFor();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	class SimplePass extends RenderPass {
		@Override
		public void initPass(JmeContext context) {
			// TODO Auto-generated method stub
			Renderer renderer = context.getRenderer();

			Camera camera = renderer.createCamera(640, 480);
			camera.setFrustumPerspective(45, 640f / 480f, 1, 10000);
			camera.setLocation(new Vector3f(100, 100, 100));
			camera.lookAt(new Vector3f(0, 0, 0), new Vector3f(0, 1, 0));
			camera.update();
			renderer.setCamera(camera);

			Node house = new Node("House Node");
			house.attachChild((Spatial) modelRepository.getRepository().get(
					"house"));
			house.setLightCombineMode(LightCombineMode.Off);
			house.setTextureCombineMode(TextureCombineMode.Replace);

			ZBufferState state = renderer.createZBufferState();
			state.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);
			house.setRenderState(state);
			house.updateModelBound();
			house.updateRenderState();
			add(house);
		}
	}

	public static void main(String s[]) {
		ModelRepository modelRepository = new ModelRepository();
		modelRepository.getLoadList().put("house", "model.txt");
		modelRepository.loadModel();
		modelRepository.waitForLoad();
		ModelPreLoaderTest loaderTest = new ModelPreLoaderTest(modelRepository);
	}
}
